package com.ruoyi.mes.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.ProInfo;
import com.ruoyi.mes.domain.ProOrder;
import com.ruoyi.mes.service.IndexService;
import com.ruoyi.mes.vo.index.OrderFactsVo;
import com.ruoyi.mes.vo.index.OrderLineVo;
import com.ruoyi.mes.vo.product.ProOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private ProOrderServiceImpl proOrderService;


    /**
     * 当日订单情况
     *
     * @return
     */
    @Override
    public OrderFactsVo orderFacts() {
        int finishCount = 0;//订单完成数
        int proDealNumber = 0;//产品交易数

        OrderFactsVo orderFactsVo = new OrderFactsVo();

        ProOrder proOrder = new ProOrder();
        Date date = DateUtils.dateTime(DateUtils.YYYY_MM_DD, DateUtils.getDate());
        proOrder.setCreateTime(date);
        List<ProOrder> proOrders = proOrderService.selectDistinctOrders(proOrder);
        if (proOrders.size() != 0) {
            orderFactsVo.setPlannedQuantity((long) proOrders.size());
            for (ProOrder order : proOrders) {
                if (order.getOrderStatus() == 1) {
                    finishCount++;

                    AjaxResult details = proOrderService.getDetails(order.getId());
                    Object o = details.get(AjaxResult.DATA_TAG);
                    ProOrderVo proOrderVo = new ProOrderVo();
                    BeanUtils.copyProperties(o, proOrderVo);

                    for (ProInfo proInfo : proOrderVo.getProInfoList()) {
                        proDealNumber += proInfo.getOrderDemandNum();
                    }
                }
            }

            orderFactsVo.setCompletedQuantity((long) finishCount);
            orderFactsVo.setProDealNumber((long) proDealNumber);
            orderFactsVo.setOrderFulfillmentRate((float) finishCount / proOrders.size() * 100);
        }

        return orderFactsVo;
    }

    /**
     * 上一周订单数
     *
     * @return
     */
    @Override
    public OrderLineVo lastWeekProOrders() {
        OrderLineVo orderLineVo = new OrderLineVo();
        List<String> date = new ArrayList<>();
        List<Long> data = new ArrayList<>(Collections.nCopies(7, 0L));


        // 计算上一周的开始时间和结束时间
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.add(Calendar.DATE, -7);
        Date startTime = calendar.getTime();
        calendar.add(Calendar.DATE, 6);
        Date endTime = calendar.getTime();

        // 初始化map，将所有日期的数量设置为0
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(startTime);
        while (calendar.getTime().before(endTime)) {
            date.add(sdf.format(calendar.getTime()));
            calendar.add(Calendar.DATE, 1);
        }
        date.add(sdf.format(endTime));
        orderLineVo.setDate(date);


        List<Map<String, Long>> maps = proOrderService.lastWeekProOrders(startTime, endTime);

        for (Map<String, Long> map : maps) {
            String day = String.valueOf(map.get("day"));
            data.set(date.indexOf(day),map.get("count"));
        }
        orderLineVo.setData(data);

        return orderLineVo;
    }

}
